using IQIGame.Onigao.Framework;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public static class BindPointUtil
    {
        /// <summary>
        /// 获取骨骼绑定点
        /// </summary>
        /// <param name="pointId"></param>
        /// <param name="rootPoint">root</param>
        /// <returns></returns>
        public static Transform GetBindPoint(int pointId, Transform rootPoint)
        {
            //业务处理缓存池
            return FindBindPoint(pointId, rootPoint);
        }

        public static Transform GetBindPoint(EntityBindPointCommonType type, Transform rootPoint)
        {
            return FindBindPoint((int)type, rootPoint);
        }

        private static Transform FindBindPoint(int pointId, Transform rootPoint)
        {
            if (pointId <= 0)
            {
                return rootPoint;
            }
            var pointCfg = TableCenter.entityBindPoint.Get(pointId);
            if (pointCfg == null)
            {
                LogGame.LogError($"根据挂点id：{pointId} 找不到对应的配置。");
                return rootPoint;
            }
            Transform rTrans = rootPoint.Find(pointCfg.Path);
            if (rTrans == null)
            {
                LogGame.LogError($"实体【{rootPoint.name}】上找不到挂点：{pointCfg.Path}。");
                return rootPoint;
            }
            return rTrans;
        }
    }
}